package com.systop.party06.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.systop.party06.R;
import com.systop.party06.activity.AboutUsActivity;
import com.systop.party06.activity.LoginActivity;
import com.systop.party06.activity.MainActivity;
import com.systop.party06.base.BaseFragment;
import com.systop.party06.entity.ComEntity;
import com.systop.party06.entity.PersonEntity;
import com.systop.party06.http.MyStringCallback;
import com.systop.party06.utils.SPUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.systop.party06.http.ComUrl.GRZX;

import static com.systop.party06.http.ComUrl.LOGIN_OUT;
import static com.systop.party06.utils.StaticDateUtils.SP_TOKEN;

public class PersonFragment extends BaseFragment {

    @BindView(R.id.person_tv_name)
    TextView personTvName;
    @BindView(R.id.person_tv_idcard)
    TextView personTvIdcard;
    @BindView(R.id.person_tv_phone)
    TextView personTvPhone;
    @BindView(R.id.person_refresh)
    SmartRefreshLayout refresh;

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_person, container, false);
        ButterKnife.bind(this, view);
        //setEnableLoadMore属性用于控制是否启用加载更多功能。该属性在最新版本中已废弃，建议通过设置 Footer 高度和触发条件实现类似功能
        refresh.setEnableLoadMore(false);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                okhttp();
            }
        });
        // 立即触发下拉刷新动画  autoRefresh(500); 则表示延迟500毫秒后触发
        refresh.autoRefresh();

        return view;
    }

    private void okhttp() {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", SPUtils.getPreference(getActivity(), SP_TOKEN));
        commonOkhttp.execute(GRZX, param, new MyStringCallback(getActivity(), false) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                refresh.finishRefresh(true);
                PersonEntity entity = gson.fromJson(response, PersonEntity.class);
                PersonEntity.DataBean yhxxBean = entity.getData();
                personTvName.setText(yhxxBean.getName());
                personTvIdcard.setText("(" + yhxxBean.getSfzh() + ")");
                personTvPhone.setText(yhxxBean.getSjh());
            }
            @Override
            public void onOther(String response, int status, String message) {
                super.onOther(response, status, message);
                refresh.finishRefresh(true);
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                refresh.finishRefresh(false);
            }
        });
    }

    @OnClick({R.id.person_ll_person, R.id.person_ll_change, R.id.person_ll_branch, R.id.person_ll_gywm, R.id.person_ll_tcxt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.person_ll_person:
                //个人信息
                break;
            case R.id.person_ll_branch:
                //我的支部
                break;
            case R.id.person_ll_change:
                //修改手机号
                break;
            case R.id.person_ll_gywm:
            Intent itent=new Intent(getActivity(), AboutUsActivity.class);
            startActivity(itent);
                //关于我们
                break;
            case R.id.person_ll_tcxt:
                //退出登录
                okhttpLoginOut();
                break;
        }
    }

    private void okhttpLoginOut() {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", SPUtils.getPreference(getActivity(), "token"));
        commonOkhttp.execute(LOGIN_OUT, param, new MyStringCallback(getActivity(), false) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                ComEntity comEntity = gson.fromJson(response, ComEntity.class);
                showMessage(comEntity.getMsg());
                SPUtils.clearSP(getActivity());
                MainActivity activity = (MainActivity) getActivity();
                activity.finishActivity();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
    }

}